<!--
	作者：offline
	时间：2019-03-20
	描述：流程列表仅查看
-->
<template>
  <div class="m-view-flowList m-window" v-loading="detailsLoading">
    <div class="m-condition" v-if="diaShow">
      <!-- 搜索条件 -->
      <el-row :gutter="10">
        <el-form
          :model="headerRuleForm"
          ref="headerRuleForm"
          label-width="70px"
          :inline-message="true"
          label-position="left"
        >
          <el-col :span="6">
            <el-input placeholder="请输入创建人或单据" v-model="inputMsg" class="setLeftSize" clearable></el-input>
          </el-col>
          <el-col :span="2">
            <el-button type="primary" @click="search(7)" class="setLeftSize">查询</el-button>
          </el-col>
          <el-col :span="2">
            <el-checkbox v-model="state" class="setLeftSize" @change="stateChange">显示已读</el-checkbox>
          </el-col>
        </el-form>
      </el-row>
    </div>
    <div class="m-agTable m-bottom m-top1" v-if="diaShow">
      <ag-grid-vue
        class="m-agTableContent ag-theme-balham"
        :gridOptions="flowGridOptions"
        :columnDefs="flowColumnDefs"
        :rowData="rowData"
        v-loading="loading"
        rowSelection="single"
      ></ag-grid-vue>
    </div>
    <div class="m-page" v-if="diaShow">
      <el-pagination
        @size-change="flowHandleSizeChange"
        @current-change="flowHandleCurrentChange"
        @prev-click="flowHandlePrevClick"
        @next-click="flowHandleNextClick"
        :current-page="flowCurrentPage"
        :page-sizes="[30,50,100,150,200]"
        :page-size="flowPageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="flowTotal"
      ></el-pagination>
    </div>

    <!-- 调用实物对账单组件 -->
    <div class="m-window" v-if="!isPhysicalShow">
      <physicalApplyComponents
        @cancelComponents="cancelComponents"
        :pageLayOut="pageLayOut"
        :setClickValue="getClickValue"
      ></physicalApplyComponents>
    </div>

    <!-- 调用会计对账单组件 -->
    <div class="m-window" v-if="!isAccountShow">
      <accountApplyComponents
        @cancelComponents="cancelComponents"
        :pageLayOut="pageLayOut"
        :setClickValue="getClickValue"
      ></accountApplyComponents>
    </div>
    <!-- 调用会计对账单审核组件 -->
    <div class="m-window" v-if="!isAccountAudit">
      <accountAuditComponents
        @cancelComponents="cancelComponents"
        :pageLayOut="pageLayOut"
        :setClickValue="getClickValue"
      ></accountAuditComponents>
    </div>
    <!-- 调用订单组件 -->
    <div class="m-window" v-if="!isPurOrdShow">
      <purOrderDetailsComponents
        @cancelComponents="cancelComponents"
        :ordStatus="ordStatus"
        :getClickValue="getClickValue"
      ></purOrderDetailsComponents>
    </div>
  </div>
</template>

<style lang="less">
.m-view-flowList {
  .m-condition {
    .lesso-logo {
      width: 50%;
    }
  }
}
</style>

<script>
import _Api from "@/api";
import agGridComponents from "@/components/agGridComponents.js"
import noticeDetailsComponent from "@/views/mine/noticeDetailsComponent.vue";
import localeText from "@/i18n/agGridText";
import constant from "@/public/constant";
import buttonLink from "@/components/buttonLink.js";
import physicalApplyComponents from "@/views/accountManagement/physicalApplyComponents.vue"
import accountApplyComponents from "@/views/accountManagement/accountApplyComponents.vue"
import accountAuditComponents from "@/views/accountManagement/accountAuditComponents"
import purOrderDetailsComponents from "@/views/purchase/purOrderDetailsComponents.vue"
import Msg from "@/public/message"


export default {
  data () {
    return {
      // 页面 ag-grid 
      flowGridOptions: {},
      defaultColDef: null,
      rowSelection: null,
      gridApi: null,
      columnApi: null,
      flowColumnDefs: [],
      selRTCodeColumnDefs: [],
      selRTCodeRowData: [],
      rowData: [],
      loading: false, //表格加载动画
      headerRuleForm: {},

      //缓存搜索条件
      tempSearch: {
        state: '',
        inputMsg: '',
      },

      flowCurrentPage: 1,
      flowPageSize: 30,
      flowTotal: 0,

      state: '',
      inputMsg: '',

      //组件调用
      isPhysicalShow: true,
      isAccountShow: true,
      isPurOrdShow: true,
      pageLayOut: 'edit',
      diaShow: true,
      ordStatus: '',
      getClickValue: {},
      // 加载详情
      detailsLoading: false,
      // 是否显示会计对账审核组件   
      isAccountAudit: true
    }
  },
  created () {
    this.search();
  },
  beforeMount () {
    // 页面 ag-grid 配置
    this.flowGridOptions = {
      enableFilter: false, //列过滤器
      enableSorting: true, //列排序

      context: {
        componentParent: this
      },

      enableColResize: true,
      localeText: localeText,
      enableCellChangeFlash: true,
      suppressRowClickSelection: true,
    };

    this.flowColumnDefs = [
      { headerName: "单据编号", field: "orderNo", width: 150, cellRenderer: buttonLink },
      { headerName: "创建日期", field: "createDate", width: 150 },
      { headerName: "创建人", field: "creator", width: 150 },
      { headerName: "工作流", field: "content", width: 300 },
      { headerName: "流程类型", field: "orderType", width: 100 },
      { headerName: "当前节点", field: "nowTarget", width: 250 }
    ];
  },
  mounted () {

  },
  methods: {
    search (flag) {
      var that = this;
      this.loading = true;
      if (flag == 7) {
        this.flowCurrentPage = 1;
      }
      let obj = {
        currentPage: this.flowCurrentPage,
        pageSize: this.flowPageSize,
        receiverId: constant.userId,
        state: this.state === true ? 1 : 0,
        keyword: this.inputMsg
      }

      _Api.getBusinessList(obj).then(function (response) {
        that.loading = false;
        var data = response.data;
        if (data.code == "1") {
          if (data.total > 0) {
            that.flowTotal = data.total;
            that.rowData = data.rows.splice(0);
            that.rowData.forEach(item => {
              let orderNo = JSON.parse(item.param);
              item.orderNo = orderNo.targetNo;
              item.orderId = orderNo.targetId;
              if (item.orderNo.indexOf('KJ') != -1) {
                item.orderType = '会计对账单'
              } else if (item.orderNo.indexOf('SW') != -1) {
                item.orderType = '实物对账单'
              } else {
                item.orderType = '订单'
              }
              item.nowTarget = constant.userName
              item.flagUI = 'flowInfo';
              //   that.getListById(item.orderId, item.orderNo);
            })
          } else {
            that.rowData = [];
            that.flowTotal = 0;
          }
        }
      }).catch(() => {
        that.flowloading = false;
      })
    },
    // 获取单据详情信息
    getListById (orderId, orderNo) {
      var that = this;
      this.isPurOrdShow = true;
      this.isAccountShow = true;
      this.isPurOrdShow = true;
      let obj = {
        id: orderId
      }
      this.detailsLoading = true
      if (orderNo.indexOf('SW') != -1) {
        // 获取实物对账单明细抬头信息
        _Api.getGoodsAccountBillById(obj).then(function (response) {
          that.detailsLoading = false
          var data = response.data;
          if (data.code == "1") {
            if (data.total > 0) {
              that.getClickValue = data.rows
              that.diaShow = false;
              that.isAccountAudit = true;
              that.isPhysicalShow = false;
              that.isAccountShow = true;
              that.isPurOrdShow = true;
            }
          } else {
            Msg.showErrorMsg(data.info)
          }
        }).catch((err) => {
          this.detailsLoading = false
          Msg.showErrorMsg(err.data.message)
        })
      } else if (orderNo.indexOf('KJ') != -1) {
        //   获取会计对账单明细抬头信息
        _Api.getAccountBillById(obj).then(function (response) {
          that.detailsLoading = false
          var data = response.data;
          if (data.code == "1") {
            if (data.total > 0) {
              that.getClickValue = data.rows
              const { billState } = data.rows
              that.diaShow = false;
              that.isPhysicalShow = true;
              that.isPurOrdShow = true;
              if (that.$global.user.userTypeId == 1) {
                if (billState > 2) {
                  that.isAccountShow = true;
                  that.isAccountAudit = false
                } else {
                  that.isAccountShow = false;
                  that.isAccountAudit = true
                }
              } else {
                that.isAccountShow = false;
                that.isAccountAudit = true
              }
            }
          } else {
            Msg.showErrorMsg(data.info)
          }
        }).catch((err) => {
          this.detailsLoading = false
          Msg.showErrorMsg(err.data.message)
        })
      }
      else {
        //  获取订单明细主表信息  
        _Api.getPurchaseOrderBillById(obj).then(function (response) {
          that.detailsLoading = false
          var data = response.data;
          if (data.code == "1") {
            if (data.total > 0) {
              if (data.rows.delFlag == 1) {
                Msg.showPopMsg('订单已被退回或被强制收回，暂不能做后续操作！')
                that.diaShow = true
                return
              }
              that.getClickValue = data.rows
              that.diaShow = false;
              that.isPhysicalShow = true;
              that.isAccountShow = true;
              that.isAccountAudit = true;
              that.isPurOrdShow = false;
              if (constant.accountType == "1") {
                if (data.rows.billState == 2) {
                  // 已发布
                  that.ordStatus = 2
                } else {
                  // 未发布
                  that.ordStatus = 1
                }
              } else if (constant.accountType == "4") {
                if (data.rows.billState == 1) {
                  //  未确认
                  that.ordStatus = 4
                } else if (data.rows.billState == 2) {
                  // 已确认
                  that.ordStatus = 3
                }
              }
            }
          } else {
            Msg.showErrorMsg(data.info)
          }
        }).catch((err) => {
          this.detailsLoading = false
          Msg.showErrorMsg(err.data.message)
        })
      }
    },

    //明细页面跳转
    setClickValue (value) {
      const { orderId, orderNo } = value.data
      console.log("表格数据", value.data)
      if (this.state != true) {
        //未读消息点击后更新状态
        this.updateFlowState(value.data.id);
      }
      this.getListById(orderId, orderNo);
    },
    cancelComponents (msg) {
      var that = this;
      if (msg) {
        this.isPhysicalShow = true;
        this.isAccountShow = true;
        this.isPurOrdShow = true;
        this.diaShow = true
        this.isAccountAudit = true
        this.$nextTick(_ => {
          that.search();
        })
      }
    },

    //查询状态修改
    stateChange () {
      this.search();
    },

    //分页相关
    flowHandleSizeChange (val) {
      this.flowPageSize = val;
      //   this.getTempSearch();
      this.search();
    },
    flowHandleCurrentChange (val) {
      this.flowCurrentPage = val;
      //   this.getTempSearch();
      this.search();
    },
    flowHandlePrevClick () {
      this.flowCurrentPage = this.flowCurrentPage - 1;
      //   this.getTempSearch();
    },
    flowHandleNextClick () {
      this.flowCurrentPage += 1;
      //   this.getTempSearch();
    },
    // 缓存搜索条件
    saveTempSearch () {
      this.tempSearch.state = this.state;
      this.tempSearch.inputMsg = this.inputMsg;
    },
    // 读取缓存搜索条件
    getTempSearch () {
      this.state = this.tempSearch.state;
      this.inputMsg = this.tempSearch.inputMsg;
    },
    //更新流程状态
    updateFlowState (id) {
      let obj = {
        data: id
      }

      _Api.updateBusinessState(obj).then(function (response) {
        var data = response.data;
        if (data.code == "1") {
          console.log("更新流程状态成功！");
        }
      }).catch(() => { })
    }
  },

  components: {
    noticeDetailsComponent,
    physicalApplyComponents,
    accountApplyComponents,
    purOrderDetailsComponents,
    agGridComponents,
    accountAuditComponents
  }
};
</script>


